﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using 开单.Dao.基础;
using 开单.Model.基础;
using 开单.ComMethod;
using 开单.DB;

namespace 开单.Frm.基础
{
    public partial class FrmYgxx : Form
    {
        YgxxDao ygxxDao = new YgxxDao();
        YgxxEntity ygxxEntity = new YgxxEntity();
        DataTable dt = new DataTable();
        private static FrmYgxx Instance;
        private FrmYgxx()
        {
            InitializeComponent();
            this.uClistMoreYg.ListEvent += new ClientApplication.ComControl.UClistMore.ListEventHandler(uClistMoreYg_ListEvent);
            RefeashDtv();
        }

        public static FrmYgxx GetInstance()
        {
            if (null == Instance || Instance.IsDisposed)
            {
                Instance = new FrmYgxx();
                //Instance.MdiParent = GlobalVar.frmMain;
            }
            Instance.BringToFront();
            return Instance;
        }

        void uClistMoreYg_ListEvent(开单.ComMethod.MyListItem myList)
        {
            TxtYg.Text = myList.TextField;
            TxtYg.Tag = myList.ValueField;
            uClistMoreYg.Visible = false;
            TxtYg.Focus();
        }

        private void RefeashDtv()
        {
            DataTable dtAll= ygxxDao.GetYgxxDt("");
            dataGridView1.DataSource = dtAll;
            dtAll.Dispose();
        }

        private void toolStripQuery_Click(object sender, EventArgs e)
        {
            if (null != dt)
            {
                dt = ygxxDao.GetYgxxDtByYgName(TxtYg.Text.ToString());
                dataGridView1.DataSource = dt;
            }
        }

        private void TxtYg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (0 != dt.Rows.Count)
                {
                    if (dt.Rows[0][1].ToString() == TxtYg.Text) { return; }
                }
                dt = ygxxDao.GetYgxxDt(TxtYg.Text.Trim());
                uClistMoreYg.addData(dt);
                uClistMoreYg.BringToFront();
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.RowCount == 0) { return; }
            if (e.RowIndex == -1) { return; }
            TxtYg.Tag = dataGridView1[0, e.RowIndex].Value;
            TxtYgName.Text = dataGridView1[1, e.RowIndex].Value.ToString();
            TxtYgjp.Text = dataGridView1[2, e.RowIndex].Value.ToString();
            groupBox3.Visible = true;
            button2.Visible = false;
            button1.Visible = true;
            toolStripDel.Visible = true;
        }

        private void toolStripRefeash_Click(object sender, EventArgs e)
        {
            RefeashDtv();
            groupBox3.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!(MessageBox.Show("确定要修改吗？", "提示", MessageBoxButtons.YesNo,
                      MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)){ return; }
            ygxxEntity.yg_code = Convert.ToInt32(TxtYg.Tag.ToString());
            ygxxEntity.yg_name = TxtYgName.Text;
            ygxxEntity.yg_jp = TxtYgjp.Text;
            ygxxDao.UpdataYgxxByYg_code(ygxxEntity);
            RefeashDtv();
            MessageBox.Show("修改成功");
            IntityControl();
            groupBox3.Visible = false;
        }

        private void TxtYgName_TextChanged(object sender, EventArgs e)
        {
            this.TxtYgjp.Text = GetPYString.GetPYStr(TxtYgName.Text);
        }

        private void toolStripClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripAdd_Click(object sender, EventArgs e)
        {
            groupBox3.Visible = true;
            button2.Visible = true;
            button1.Visible = false;
            TxtYgName.Focus();
            IntityControl();
        }

        private void IntityControl()
        {
            TxtYgName.Text = "";
            TxtYgjp.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!(IsOkToAdd())) { return; }
            if (!(MessageBox.Show("确定要增加吗？", "提示", MessageBoxButtons.YesNo,
          MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)) { return; }
            //ygxxEntity.yg_code = Convert.ToInt32(TxtYg.Tag.ToString());
            ygxxEntity.yg_name = TxtYgName.Text;
            ygxxEntity.yg_jp = TxtYgjp.Text;
            ygxxDao.AddYgxx(ygxxEntity);
            RefeashDtv();
            MessageBox.Show("增加成功");
            IntityControl();
        }

        private bool IsOkToAdd()
        {
            if (TxtYgName.Text == "") { MessageBox.Show("请先输入员工名"); TxtYgName.Focus(); return false; }
            return true;
        }

        private void toolStripDel_Click(object sender, EventArgs e)
        {
            if (!(MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo,
            MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)) { return; }
            if (dataGridView1.RowCount == 0 || dataGridView1.CurrentCell.RowIndex == -1) { return; }
            ygxxDao.DelYgxx(TxtYg.Tag);
            RefeashDtv();
            MessageBox.Show("删除成功");
            groupBox3.Visible = false;
            IntityControl();
        }
    }
}
